package com.situ.crm.rbac.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.situ.crm.common.util.Result;
import com.situ.crm.rbac.pojo.User;
import com.situ.crm.rbac.service.IUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import springfox.documentation.annotations.ApiIgnore;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author Gao
 * @since 2023-10-19
 */
@Api(tags = "用户管理") //tags：你可以当作是这个组的名字。
@RestController
@RequestMapping("/rbac/user")
public class UserController {
    @Autowired
    private IUserService userService;

    // 注意，对于swagger，不要使用@RequestMapping，
    // 因为@RequestMapping支持任意请求方式，swagger会为这个接口生成7种请求方式的接口文档
    // value：可以当作是接口的简称 notes：接口的描述
    @ApiOperation(value = "登录",notes = "输入用户名和密码验证是否成功")
    @PostMapping("/login")
    public Result login(String username, String password) {
        Map<String, String> map = new HashMap<>();
        map.put("token", "admin");
        return Result.ok(map);
    }

    @GetMapping("/info")
    public Result info(){
        Map<String, String> map = new HashMap<>();
        map.put("name", "admin");
        map.put("avatar", "https://wpimg.wallstcn.com/f778738c-e4f8-4870-b634-56703b4acafe.gif");
        return Result.ok(map);
    }

    @ApiOperation(value = "分页", notes = "根据第几页，每页有多少条查询指定页数据")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value="第几页", required = false, defaultValue = "1"),
            @ApiImplicitParam(name = "limit", value="每页有多少条", required = false, defaultValue = "5")
    })
    @GetMapping("/selectByPage")
    public Result selectByPage(@RequestParam(defaultValue = "1") Integer page,@RequestParam(defaultValue = "5") Integer limit) {
        IPage<User> iPage = new Page<>(page, limit);
        userService.page(iPage, null);
        HashMap<String, Object> map = new HashMap<>();
        map.put("total", iPage.getTotal());
        map.put("list", iPage.getRecords());
        return Result.ok(map);
    }

    @ApiIgnore
    @GetMapping("/selectAll")
    @RequestMapping(method = RequestMethod.DELETE)
    public Result selectAll() {
        List<User> list = userService.list();
        return Result.ok(list);
    }

    @DeleteMapping("/deleteById/{id}")
    public Result deleteById(@PathVariable Long id) {
        boolean isSuccess = userService.removeById(id);
        return isSuccess ? Result.ok("删除成功") : Result.error("删除失败");
    }

    @PostMapping("/add")
    public Result add(@RequestBody User user) {
        boolean isSuccess = userService.saveOrUpdate(user);
        return isSuccess ? Result.ok("添加成功") : Result.error("添加失败");
    }

    @GetMapping("/selectById")
    public Result selectById(Long id) {
        User user = userService.getById(id);
        return Result.ok(user);
    }

    @PutMapping("/update")
    public Result update(@RequestBody User user) {
        boolean isSuccess = userService.saveOrUpdate(user);
        return isSuccess ? Result.ok("更新成功") : Result.error("更新失败");
    }
}

